﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Incubator.Controls
{
    public partial class admin : System.Web.UI.UserControl
    {
        List<RadioButton> rb_list = new List<RadioButton>();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["UserID"] == null || (int)Session["UserID"] == -1)
                Response.Redirect("main.aspx");
            //webRef.IncubatorService serv = new Incubator.webRef.IncubatorService();

            //Web Service Needed
            webRef.User[] users = Global.incuServ.GetAllUsers();
            //serv.geta
            //show them in the table

            const string group_name = "Usergroup";

            rb_list.Clear();

            foreach (webRef.User u in users)
            {
                if (u.company_id == 0)
                    continue;
                TableCell tc0 = new TableCell();
                TableCell tc1 = new TableCell();
                TableCell tc2 = new TableCell();
                TableCell tc3 = new TableCell();
                TableCell tc4 = new TableCell();

                TableRow tr = new TableRow();

                RadioButton rb = new RadioButton();
                rb.Text = u.username;
                rb.Attributes.Add("userid", u.id.ToString());
                rb.GroupName = group_name;
                rb_list.Add(rb);

                tc0.Controls.Add(rb);

                tc1.Controls.Add(new LiteralControl(u.lname +" " + u.fname));
              
                webRef.Company c = Global.incuServ.GetCompany(u.company_id);
                tc2.Controls.Add(new LiteralControl(c.name) );
                tc3.Controls.Add(new LiteralControl(c.money.ToString()) );
                tc4.Controls.Add(new LiteralControl(u.role_id != 0 ? "Admin" : "User"));
               

                tr.Cells.Add(tc0);
                tr.Cells.Add(tc1);
                tr.Cells.Add(tc2);
                tr.Cells.Add(tc3);
                tr.Cells.Add(tc4);
                tblUsers.Rows.Add(tr);
            }
        }

        //delete user
        protected void Button1_Click(object sender, EventArgs e)
        {
            //know which share is selected
            //get list of shares from market place
             //webRef.IncubatorService serv = new Incubator.webRef.IncubatorService();

            webRef.User[] users = Global.incuServ.GetAllUsers();

             //know which share is selected
             int i = 0;
             foreach (RadioButton rb in rb_list)
             {
                 if (rb.Checked)
                 {
                     //delete specific user
                     Global.incuServ.DeleteUser(Global.incuServ.GetUser(Int32.Parse(rb.Attributes["userid"])));
                     Response.Redirect("Admin.aspx");
                     return;
                 }
                 i++;
             }
             //no radio button has been selected.
        }

        //promote user
        protected void Button2_Click(object sender, EventArgs e)
        {
            //know which share is selected
            //get list of shares from market place
             //webRef.IncubatorService serv = new Incubator.webRef.IncubatorService();

            webRef.User[] users = Global.incuServ.GetAllUsers();

             //know which share is selected
             int i = 0;
             foreach (RadioButton rb in rb_list)
             {
                 if (rb.Checked)
                 {
                     //promote user to admin
                     Global.incuServ.SetUserRole(Global.incuServ.GetUser(Int32.Parse(rb.Attributes["userid"])), 1);
                     Response.Redirect("Admin.aspx");
                     return;
                 }
                 i++;
             }
            //no radio button has been selected.
        }
        //change money
        protected void btnMoney_Click(object sender, EventArgs e)
        {
            //know which share is selected
            //get list of shares from market place
             //webRef.IncubatorService serv = new Incubator.webRef.IncubatorService();

            webRef.User[] users = Global.incuServ.GetAllUsers();

             //know which share is selected
             int i = 0;
             foreach (RadioButton rb in rb_list)
             {
                 if (rb.Checked)
                 {
                     webRef.Company c = Global.incuServ.GetCompany(Global.incuServ.GetUser(Int32.Parse(rb.Attributes["userid"])).company_id);
                     //delete specific user
                     Global.incuServ.SetMoney(c, Int32.Parse(tbMoney.Text));
                     Response.Redirect("Admin.aspx");
                     return;
                 }
                 i++;
             }
             //no radio button has been selected.
       

        }
    }
}